package cn.uin.trigger.http;

import cn.uin.api.IIndexService;
import cn.uin.api.dto.request.family.CreateFamilyRequestDTO;
import cn.uin.api.dto.response.family.UserFamilyInfoResponse;
import cn.uin.api.dto.response.oss.UploadFileResponse;
import cn.uin.api.response.Response;
import cn.uin.domain.family.model.aggregate.UserFamilyInfoAggregate;
import cn.uin.domain.family.model.entity.HomeFamilyEntity;
import cn.uin.domain.family.model.entity.HomeFamilyListEntity;
import cn.uin.domain.family.service.IFamilyService;
import cn.uin.types.enums.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName FamilyController
 * @Description:首页服务接口
 * @Author: Administrator
 * @CreateDate: 2025/11/11 17:29
 * @Version: 1.0
 */
@Slf4j
@RestController()
@CrossOrigin("*")
@RequestMapping("/api/index/")
public class IndexController implements IIndexService {

	@Resource
	private IFamilyService iFamilyService;

	@Override
	@RequestMapping(value = "queryUserFamilyInfo", method = RequestMethod.POST)
	public Response<UserFamilyInfoResponse> queryUserFamilyInfo(Long userId) {
		UserFamilyInfoAggregate userFamilyInfoAggregate = iFamilyService.queryUserFamilyInfo(userId);
		HomeFamilyEntity homeFamilyEntity = userFamilyInfoAggregate.getHomeFamilyEntity();
		HomeFamilyListEntity currentUserHomeFamilyListEntity = userFamilyInfoAggregate.getCurrentUserHomeFamilyListEntity();
		List<HomeFamilyListEntity> homeFamilyListsEntity = userFamilyInfoAggregate.getHomeFamilyListsEntity();

		// 当前用户家庭成员信息
		UserFamilyInfoResponse.FamilyList currentUserFamilyInfo = UserFamilyInfoResponse.FamilyList.builder().build();
		if (!ObjectUtils.isEmpty(currentUserHomeFamilyListEntity)) {
			BeanUtils.copyProperties(currentUserHomeFamilyListEntity, currentUserFamilyInfo);
		}

		// 家庭其他成员信息
		List<UserFamilyInfoResponse.FamilyList> otherHomeFamilyLists = null;
		if (!CollectionUtils.isEmpty(homeFamilyListsEntity)) {
			otherHomeFamilyLists = homeFamilyListsEntity.stream().map(item -> {
				UserFamilyInfoResponse.FamilyList otherFamilyMembers = UserFamilyInfoResponse.FamilyList.builder().build();
				BeanUtils.copyProperties(item, otherFamilyMembers);
				return otherFamilyMembers;
			}).collect(Collectors.toList());
		}

		// 构建返回对象
		UserFamilyInfoResponse userFamilyInfoResponse = UserFamilyInfoResponse.builder()
				.name(homeFamilyEntity.getName())
				.headImage(homeFamilyEntity.getHeadImage())
				.num(homeFamilyEntity.getNum())
				.desc(homeFamilyEntity.getDesc())
				.homeFamilyListEntity(currentUserFamilyInfo)
				.homeFamilyListsEntity(otherHomeFamilyLists)
				.build();
		return Response.<UserFamilyInfoResponse>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(ResponseCode.SUCCESS.getInfo())
				.data(userFamilyInfoResponse)
				.build();
	}

	@Override
	@RequestMapping(value = "inviteToFamily", method = RequestMethod.POST)
	public Response<Boolean> inviteToFamily(@RequestParam Long family, @RequestParam String email) {
		Boolean inviteFlag = iFamilyService.inviteToFamily(family, email);
		return Response.<Boolean>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(inviteFlag ? ResponseCode.SUCCESS.getInfo(): ResponseCode.C0004.getInfo())
				.data(inviteFlag)
				.build();
	}

	@Override
	@RequestMapping(value = "createFamily", method = RequestMethod.POST)
	public Response<Boolean> createFamily(CreateFamilyRequestDTO createFamilyRequestDTO) {
		HomeFamilyEntity homeFamilyEntity = HomeFamilyEntity.builder().build();
		BeanUtils.copyProperties(createFamilyRequestDTO, homeFamilyEntity);
		iFamilyService.createFamily(homeFamilyEntity);
		return Response.<Boolean>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(ResponseCode.SUCCESS.getInfo())
				.build();
	}

}
